package fun.boomcat.luckyhe.mirai.plugin.luckymoney.command;

import fun.boomcat.luckyhe.mirai.plugin.luckymoney.exception.MoneyNotEnoughException;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.service.MoneyService;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.service.impl.MoneyServiceImpl;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.util.ConfigUtil;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.util.MoneyCommandStr;
import fun.boomcat.luckyhe.mirai.plugin.luckymoney.util.SendMessageUtil;
import kotlin.Unit;
import kotlin.coroutines.Continuation;
import net.mamoe.mirai.console.command.CommandOwner;
import net.mamoe.mirai.console.command.CommandSender;
import net.mamoe.mirai.console.command.RawCommand;
import net.mamoe.mirai.console.permission.Permission;
import net.mamoe.mirai.contact.User;
import net.mamoe.mirai.message.data.At;
import net.mamoe.mirai.message.data.MessageChain;
import net.mamoe.mirai.message.data.MessageChainBuilder;
import org.apache.ibatis.exceptions.PersistenceException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import sun.security.krb5.Config;

import java.io.IOException;
import java.util.Arrays;

/**
 * @author Lucky_He
 * @date 2021年9月5日 14:34
 * @description MoneyCommand
 */
public class MoneyCommand extends RawCommand {

    public MoneyCommand(@NotNull CommandOwner owner, @NotNull String primaryName, @NotNull String[] secondaryNames, @NotNull String usage, @NotNull String description, @NotNull Permission parentPermission, boolean prefixOptional) {
        super(owner, primaryName, secondaryNames, usage, description, parentPermission, prefixOptional);
    }

    @Nullable
    @Override
    public Object onCommand(@NotNull CommandSender commandSender, @NotNull MessageChain messageChain, @NotNull Continuation<? super Unit> continuation) {
        int mcLen = messageChain.size();
        String primaryName = getPrimaryName();
        String[] secondaryNames = getSecondaryNames();

        if (mcLen == 0) {
            MoneyService service = new MoneyServiceImpl();
            User user = commandSender.getUser();
            long qq = user == null ? 0 : user.getId();

            int currentMoney;
            try {
                currentMoney = service.getUserMoneyByQq(qq).getMoney();
            } catch (Exception e) {
                SendMessageUtil.sendMessage(commandSender, "发生错误，请稍后重试");
                return null;
            }

            try {
                SendMessageUtil.sendMessage(commandSender, "当前有：" + currentMoney + " " +
                        ConfigUtil.getMoneyUnit() + "\n" + MoneyCommandStr.getCommandHelp(primaryName, secondaryNames));
            } catch (Exception e) {
                SendMessageUtil.sendMessage(commandSender, "错误：" + e.toString());
            }

            return null;
        }

        String operation = messageChain.get(0).toString();
        switch (operation.toLowerCase()) {
            case "pay":
            case "give":
            case "付款":
            case "转账":
                onPay(commandSender, Arrays.copyOfRange(messageChain.toArray(), 1, mcLen), primaryName);
                break;
            default:

                SendMessageUtil.sendMessage(commandSender, MoneyCommandStr.getCommandHelp(primaryName, secondaryNames));
                break;
        }

        return 0;
    }

    private void onPay(CommandSender commandSender, Object[] args, String primaryName) {
        Object qqObject, moneyObject;
        try {
            qqObject = args[0];
            moneyObject = args[1];
        } catch (Exception e) {
            SendMessageUtil.sendMessage(commandSender, "错误：" + e.toString());
            return;
        }

        long qq;
        if (qqObject instanceof At) {
            qq = ((At) qqObject).getTarget();
        } else {
            try {
                qq = Long.parseLong(qqObject.toString());
            } catch (Exception e) {
                SendMessageUtil.sendMessage(commandSender, "错误：" + e.toString());
                return;
            }
        }

        int money;
        try {
            money = Integer.parseInt(moneyObject.toString());
        } catch (Exception e) {
            SendMessageUtil.sendMessage(commandSender, "错误：" + e.toString());
            return;
        }

        long senderQq = commandSender.getUser() == null ? 0L : commandSender.getUser().getId();

        MoneyService service = new MoneyServiceImpl();
        try {
            service.payUserMoneyByQq(senderQq, qq, money);
        } catch (MoneyNotEnoughException e) {
            SendMessageUtil.sendMessage(commandSender, "错误：" + e.toString());
            return;
        } catch (Exception e) {
            SendMessageUtil.sendMessage(commandSender, "发生错误，请稍后重试");
            return;
        }

        try {
            SendMessageUtil.sendMessage(commandSender, "已支出" + " " + money + " " + ConfigUtil.getMoneyUnit() +
                    "给成员" + qq + "，现在剩余 " +
                    service.getUserMoneyByQq(senderQq).getMoney() + " " + ConfigUtil.getMoneyUnit());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
